I want to implement a daily STREAKS system into my game.
So if a player plays five days in a row, my game will know it.
Something will pop up.
Say congrats, maybe give them a prize, you know, per day or something.
But with that in place, we can also figure out daily rewards.
So we're going to know when there was a day rollover.
You can give him a reward for that.
That's very popular in games.
And finally, with this system in place, we're going to know if it's the first time somebody played.
So then we could use that information to pop up instructions for first time players.
So let's go ahead and get started with that.
Go to server script service and then open up the game manager in server script service.
And we have our function ad board, which is slightly misleading because we do add the board, but we
have other player info that does not go on the board.
We're going to add two more values to that player info.
We're going to say local saved type time equals instance new.
It's going to be a number value and that information is going to go on player info and let's give saved
time a name I'm going to call it saved time.
All one word.
I'm not going to do this space because then I can't use the dot notation.
All right.
I need another variable to I'm going to do one for streak, right?
That's my daily streak instance new.
That's an int value because it's going to be like one, two, three, four.
That's also going to go on the player info.
We will give that name streak, dot name, capital streak.
Let's scroll down to initialize player data.
There it is.
Now we have two situations.
The player might have data and this is an update, right?
So we may have people playing already.
We don't want to reset their data.
We have to take into account for their old data and then we have new people.
That's not going to be a problem.
As soon as they come in.
We can give them two new values.
We can give them the streak which will set the one, and then we have our saved time and then that will
set to OSS time, which is the number of seconds after midnight of January 1st, 1970.
Epoch All right.
But this is going to be a problem.
Let's go ahead and check.
Do they have streak data or don't they have streak data?
Because we're only going to have to initialize that one time if they don't have it.
So I'm going to say, if not data dot streak that's been saved off.
Right, then what I'm going to do is I want to get my player, not the leader stats.
I'm going to do the player info and I'm going to get my street value street value set that equal to
one, right?
Because this is brand new for these people.
They just came in since our patch.
Do a control C do a control V, this is going to be saved time saved time.
That value is also going to be O's dot time.
All right?
And then the next time they come in, they're already going to have that data.
So we don't want to do that.
We're going to do an else.
Let's copy this.
I will put real values there now.
Right.
So now we don't need that one.
We could do a data dot streak.
It's going to be there and we'll do a data dot saved time.
All right.
That's looking good.
We want to check here for our daily streak.
Let's do a check, daily streak and we'll pass in the player.
We don't have that function yet.
I think I'll copy it.
Control C so I don't have to worry about spelling it wrong.
As long as you're above initialized player data, you'll be all right.
I'm going to go below the ad board function, right.
I just want it closer to the top.
Let me do a local function check daily streak.
I just did a paste there.
Let's get a couple variables.
I'll do one for saved time.
We'll go get our player, get the player info from the player, and then we're going to get the saved
time dot value.
I'm going to need the seconds per day and that's 86400 seconds in a day.
What else do I need?
I need a variable for now.
The time now I'm going to use O's time.
That's going to give me the number of seconds passed January 1st, 1970, which is Unix epoch.
It's down to the millisecond, but Lua uses the uses seconds for the unit.
All right, I'm going to do another variable called I'll call this now minus one day, and that's going
to be now minus seconds per day.
So since now as in some sort of second units since 1970.
Subtracting off 86,400 puts us back a day.
I need two more variables.
I need one for the formatted saved time.
Those seconds are great, but it's kind of tough for days and stuff like that.
So let's use OS day to format our time.
We have that little single quote star DT.
That means we're going to make a table with saved data.
So we're passing the second send.
We're going to get back right here a table with things like the year, the day, the month, the hour
is going to be very convenient.
We're going to need another one for R.
We'll call this formatted now minus one day O's date star T for our table.
And we're going to put now minus one day in there.
Now we can make comparisons on days really easy.
Let's do that.
I'm going to do an if statement right here.
I'm going to say if formatted save time dot why day?
So why day is the day of the year between one and 365 on a normal year one and 366 on a leap year.
But that way I don't have to worry about months and stuff like that.
I have a nice consecutive ordering of the days.
I'll say equals equals formatted now minus one day.
Why day then?
Well, if I subtract a day and the last time I logged in is that same day, I'm going to say that's
a good candidate to increase my daily streak.
So if I have my player dot player info dot streak, dot value plus equals one, that should be pretty
good.
We have some more stuff that to check though, but we don't want that to happen.
We're going to call this periodically, so we don't want this to happen more than once during gameplay
for the same day.
So what I'm going to do is I can get my player player info saved time, dot value equals.
Now then when I save that off, this is going to fail because there's no way that now is the same as
now minus one day.
But we have a few more things to check for.
We have our days good, but we also have to make sure that it's not like a year and a day later.
Let's go ahead.
And why did it do random?
Let's do an and get rid of that.
Go to the next line.
I'm going to do another check to make sure that the years are equal.
So we'll do a year here.
And a year here.
What happens on year rollover?
Well, we went back a day.
So it should be like if if it's December 31st, 2021.
Right.
And we're playing January 1st, we're going to subtract a day.
So these should both be 2021, these should both be 365.
I don't think it was a leap year, but that part will be.
OC Let's go ahead and put this then on the same line.
Let's add two more conditions to our F statement.
So the first condition we are one day apart from our save time and our log in time are now time we award
another value to the streak, but we'll do an LCF.
What happens if the now time minus the save time is actually less than seconds per day?
Let's do less than or equal to just to include that last second.
Well, nothing happens, right?
Because what's going to happen we're going to get down here.
The save time is going to be updated to now time.
We're going to be playing.
We're going to call this many times.
Let's just write.
Playing the same day.
All right.
If this is if this fails.
Right.
So we're not a day apart.
If this fails, we are not within 24 hours now.
We'll break the streak.
So I'll say else, just grab this instead of adding the one where you're going to set it equal to one.
And that's going to break our streak.
Let's get a print statement in here.
We'll say added to the streak and then I will put the value of the streak, the new value.
Cool.
And then here we're going to break the streak.
Where should we do it?
Underneath.
Yeah, we'll just say broke the streak.
You broke your streak
with an exclamation.
We don't want to miss it.
Lots of exclamations.
All right, so this is the boring one.
This is the one that's going to happen the most.
Let's scroll down a little bit.
And in our save player data.
Right.
We have to update.
We're going to do an update every time we call save player data, which is periodic.
It's in the save loop and when we leave.
Another thing I'm going to do on that save loop, let's just increase this to 10 seconds because I'm
getting a lot of warnings about the buffer getting filled with our save requests.
So every 10 seconds, save player data is going to happen.
We'll do our check daily streak, pass in the player.
Then we'll get our data.
And data has saved time, right?
Player dot player info save time.
And then we have the streak right streak player dot player info streak.
Oh and these are values dot value dot value.
So we're going to put that in our data.
We're going to save it off.
It'll be great.
And then save loop is now 10 seconds.
That's going to call our save player data.
Let's try this for errors.
You're not going to get exactly the same thing because I've tried this a few times off camera, but
check for errors.
I'll go to view output.
And we can go ahead and clear that.
Hit the play.
And we're not getting expand.
Our air's waves starting.
Playing the same day.
Very good.
So we're playing the same day.
We're not getting any errors.
Let's go ahead and do a little dummy test.
This is in no way comprehensive.
You'll have to test this over and over.
And then you're still going to have to really be careful about the year.
Roll over.
Let me go up to let's look at initialized player data and right here.
So we're going to get data because we already saved it off.
Let's subtract our 86400 seconds.
All right?
And that way we should increase by one day.
Let's hit our play.
Added two straight.
So we have two.
Good.
What happens if we.
Do this.
Let's do three times.
Don't forget, we saved off data.
That was.
That was a little bit older.
We should blow our streak here.
You broke your streak.
All right.
That's looking pretty good.
I'm going to try and do it tomorrow.
Log in before I post this video.
I'll do a couple of days just to make sure there's no quirks in it, but I think we're good to go.